*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	Estimate tests for bias in OLS VAM
*	----------------------------------------------------------------------------
*	IN: 	[analysis file]
*
*	OUT:	//name of output
			local outname 		"${city}_test_\`y'_het\`het'.csv"
			//name of sheet in deck
			local sheetname 	"${city}_test_\`y'_het\`het'"
*	----------------------------------------------------------------------------

	args sample sch_res ptype het bw
	tokenize `sample', parse("_")
	local years "`5'`6'`7'`8'"

	* pick VAMs, bins, subjects to test
	if `het' {
		local vams POLSVAM_lag OLSVAM_lag POLSVAM_flag POLSVAM_flag_yr POLSVAM_flag_sp POLSVAM_flag_fr POLSVAM_flag_sc POLSVAM_lag_yr POLSVAM_lag_sp POLSVAM_lag_fr ///
			POLSVAM_lag_sc OLSVAM_flag OLSVAM_flag_yr OLSVAM_flag_sp OLSVAM_flag_fr OLSVAM_flag_sc OLSVAM_lag_yr OLSVAM_lag_sp OLSVAM_lag_fr OLSVAM_lag_sc
		local binnum 20
		local tests "m"
	}
	else {
		local vams OLSVAM_func OLSVAM_flag POLSVAM_func POLSVAM_flag
		local binnum 4 10 20
		local tests "m e"
	}

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


*** test stats

	foreach y in `tests' {

		cap log close
		log using "${log}`sheetname'.smcl", replace

		use "${builddata}${city}_analysisfile_vam`sample'`sch_res'`bw'.dta", clear

		//rename VAMs
		rename *OLSVAM*_`y'_* *OLSVAM*_*
		rename *OLSVAM*_`ptype'_* *OLSVAM*_*

	*	OLS-VAM

		local rename
		foreach m of varlist `vams' {
			local rename `rename' `m' OLSVAM

			foreach bins in `binnum' {

				preserve

				//bin and set vars
				if `bins'==0{
					ivset, y(`y') ptype(`ptype')
						local Z=r(Z)
						local p=r(P)
						local S=r(S)
						local Y=r(Y)
				}
				else{
					ivset, ptype(`ptype')
						local S=r(S)
						vambin if `S', n(`bins') ptype(`ptype') vam(b_OLSVAM_flag)
					ivset, y(`y') ptype(`ptype') bin
						local Z=r(Z)
						local p=r(P)
						local S=r(S)
						local Y=r(Y)
				}

				//count kids in full sample (same as VAM estimation sample)
				qui count if stu
				local N_vam = r(N)

				**	2SLS

					eststo `m'_b`bins'_tsls: ivreg2 `Y' (`m' = `Z') `p' $iv_controls if `S', small partial(`p' $iv_controls)
					local Z = e(exexog1)
					local test_sample = e(N)

				*	forecast bias test

					test `m' == 1
					local forecast = r(F)
					if "`forecast'"=="" local forecast = r(chi2)
					local forecast_p = r(p)

				*	store

					estimates restore `m'_b`bins'_tsls

					//first stage F-stat
					estadd scalar first_F = e(widstat)

					//Sargan over-id (adjusting for residual variance dof)
					scalar overid = (e(N)-e(df_m)-1)/e(N) * e(j)
					estadd scalar overid
					estadd scalar overid_p = chi2tail(e(jdf),overid)
					estadd scalar overid_df = e(jdf)

					//forecast test
					estadd scalar forecast = `forecast'
					estadd scalar forecast_p = `forecast_p'

					//Omnibus
					scalar omnibus = overid + `forecast'
					scalar omnibus_p = chi2tail(e(jdf)+1,omnibus)
					scalar omnibus_df = e(jdf)+1
					estadd scalar omnibus
					estadd scalar omnibus_p
					estadd scalar omnibus_df

					//VAM estimation sample
					if strpos("`m'","_lag") > 0 estadd scalar N_vam = `test_sample'
					else estadd scalar N_vam = `N_vam'

				restore
			}
		}

	***	output

		esttab2 using "${tables}/raw/`outname'", replace se ///
			rename(`rename') keep(OLSVAM) ///
			stats(N first_F overid overid_p overid_df forecast forecast_p omnibus omnibus_p omnibus_df N_vam) ///
			mtitles nonumbers bfmt(j) se(j) sfmt(j)
		preserve
			insheet using "${tables}raw/`outname'", clear nonames
			export excel using "${tables}${city}_tabs_`c(username)'_`c(current_date)'.xlsx", sheetreplace sheet("`sheetname'")
			estimates clear
		restore

		log close
	}
